package de.tum.in.eist.poll.client.gui;

import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;

public class LectureVotePanel extends FlexTable {

	public static final int VOTES = 5;
	public static final String POLL_QUESTION = "How satisfied are you with this lecture?";
	public static final String POSITIVE_QUESTION = "What did you like most?";
	public static final String NEGATIVE_QUESTION = "What did you dislike or did not understand at all?";
	public static final String[] ANSWER = { "Very satisfied", "Satisfied", 
			"Neutral", "Unsatisfied", "Very unsatisfied"};
	public static final String[] SMILEY = {"/images/ClapSmiley.png", "/images/HappySmiley.png", "/images/NeutralSmiley.png", "/images/SadSmiley.png", "/images/PissedOffSmiley.png"};
	private RadioButton[] vote;
	private TextArea positiveTextArea;
	private TextArea negativeTextArea;
	private int pollGroup;

	public LectureVotePanel(int pollGroup) {
		this.pollGroup = pollGroup;
		init();
		addContent();
	}

	private void init() {
		FocusHandler handler = new FocusHandler(){

			@Override
			public void onFocus(FocusEvent event) {
				if(event.getSource().equals(positiveTextArea) && positiveTextArea.getText().equals(POSITIVE_QUESTION)){
					positiveTextArea.setText("");
				} else  if (positiveTextArea.getText().equals("")){
					positiveTextArea.setText(POSITIVE_QUESTION);
				}
				if(event.getSource().equals(negativeTextArea) && negativeTextArea.getText().equals(NEGATIVE_QUESTION)){
					negativeTextArea.setText("");
				} else  if (negativeTextArea.getText().equals("")){
					negativeTextArea.setText(NEGATIVE_QUESTION);
				}
			}
			
		};
		vote = new RadioButton[LectureVotePanel.VOTES];

		for (int i = 0; i < LectureVotePanel.VOTES; i++){
			vote[i] = new RadioButton(pollGroup +"");
			vote[i].addFocusHandler(handler);
		}
		positiveTextArea = new TextArea();
		positiveTextArea.setText(POSITIVE_QUESTION);
		negativeTextArea = new TextArea();
		negativeTextArea.setText(NEGATIVE_QUESTION);
		
		positiveTextArea.addFocusHandler(handler);
		negativeTextArea.addFocusHandler(handler);
		positiveTextArea.setName(POSITIVE_QUESTION);
	}

	private void addContent() {
		addStyleName("voteForm");
		
		int row = 0;
		int column = 0;
		final int columns = 5;

		// Add poll question
		setText(row, column, POLL_QUESTION);
		getFlexCellFormatter().setColSpan(row, column, columns);
		getCellFormatter().addStyleName(row, column, "voteQuestion");

		// Add Radio Buttons
		row++;
		for (column = 0; column < LectureVotePanel.VOTES; column++){
			setWidget(row, column, vote[column]);
			Image image = new Image(SMILEY[column]);
			image.setTitle(ANSWER[column]);
			setWidget(row+1, column, image);
		}
		vote[2].setValue(true); // Set vote[2] as standard
		//getRowFormatter().addStyleName(row, "voteRadioButtonRow");		
		
		//extra row for smileys
		row++;
		
		// Add positive textbox
		row++;
		column = 0;
		setWidget(row, 0, positiveTextArea);
		getFlexCellFormatter().setColSpan(row, column, columns);
		getCellFormatter().addStyleName(row, column, "voteTextbox");

		// Add negative textbox
		row++;
		column = 0;
		setWidget(row, column, negativeTextArea);
		getFlexCellFormatter().setColSpan(row, column, columns);
		getCellFormatter().addStyleName(row, column, "voteTextbox");
		
	}

	/**
	 * @return the index of the selected {@link RadioButton}
	 */
	public int getVote() {
		int i;
		for (i = 0; i < LectureVotePanel.VOTES; i++) {
			if (vote[i].getValue())
				break;
		}
		return i;
	}

	/**
	 * @return the positive answer of the user
	 */
	public String getPositiveAnswer() {
		return positiveTextArea.getText();
	}

	/**
	 * @return the negative answer of the user
	 */
	public String getNegativeAnswer() {
		return negativeTextArea.getText();
	}

	public TextArea getPositiveTextArea() {
		return positiveTextArea;
	}

	public TextArea getNegativeTextArea() {
		return negativeTextArea;
	}
}
